//
//  BrowseViewController.m
//  Pacific_Computer
//
//  Created by 范琦 on 14-12-30.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BrowseViewController.h"
#import "Infor_details_ViewController.h"

@interface BrowseViewController ()<UITableViewDataSource, UITableViewDelegate,UIAlertViewDelegate>
@property(nonatomic, retain) UITableView *table;
@property(nonatomic, retain) NSArray *arr;
@property(nonatomic, retain) NSArray *arrT;

@end

@implementation BrowseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
    self.arr = [uf objectForKey:@"now"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 60, 30);
    [button setTitle:@"清除记录" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    UIImage *image = [UIImage imageNamed:@"jilu.jpg"];
    imageView.image = image;
    self.table.backgroundView = imageView;
    imageView.userInteractionEnabled = YES;
    self.table.backgroundColor = [UIColor clearColor];
    self.table.showsVerticalScrollIndicator = NO;
    
    
}

- (void)dealloc
{
    [_table release];
    [_arr release];
    [_arrT release];
    [super dealloc];
}


- (void)clear:(UIButton *)button
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请问是否要清空浏览记录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    [alert release];
    alert.tag = 1001;
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Infor_details_ViewController *detail = [[Infor_details_ViewController alloc]init];
    detail.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    detail.dic = dic;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"a"];
    }
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    self.arrT = [dic allKeys];
    cell.textLabel.text = [self.arrT lastObject];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    
    return cell;

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
    NSMutableArray *arrr = [NSMutableArray arrayWithArray:self.arr];
    
    if (alertView.tag == 1001) {
        switch (buttonIndex) {
            case 1:
                [arrr removeAllObjects];
                self.arr = [NSArray arrayWithArray:arrr];
                [uf setObject:self.arr forKey:@"now"];
                [self.table reloadData];
                break;
                
            default:
                break;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
